import type { Router, RouteLocationNormalized } from 'vue-router'

const baseTitle: string = import.meta.env.VITE_TITLE

export function setPageTitle(route: RouteLocationNormalized) {
  const pageTitle = route.meta?.title
  if (pageTitle) {
    document.title = `${pageTitle} | ${baseTitle}`
  } else {
    document.title = baseTitle
  }
}

export function createPageTitleGuard(router: Router) {
  router.afterEach((to) => setPageTitle(to))
}
